<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=73586
-->
<head>
  <title>Test for Bug 73586</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  <style type="text/css">

  span { background: white; color: black; border: medium solid black; }

  </style>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=73586">Mozilla Bug 73586</a>
<div id="display"></div>

<div id="content" style="display: none">
  
</div>
<pre id="test">
<script class="testbody" type="text/javascript">

/** Test for Bug 73586 **/

const GREEN = "rgb(0, 128, 0)";
const LIME = "rgb(0, 255, 0)";
const BLACK = "rgb(0, 0, 0)";
const WHITE = "rgb(255, 255, 255)";

function cs(elt) { return getComputedStyle(elt, ""); }

function check_children(p, check_cb) {
    var len = p.childNodes.length;
    var elts = 0;
    var i, elt, child;
    for (i = 0; i < len; ++i) {
      if (p.childNodes[i].nodeType == Node.ELEMENT_NODE)
        ++elts;
    }

    elt = 0;
    for (i = 0; i < len; ++i) {
        child = p.childNodes[i];
        if (child.nodeType != Node.ELEMENT_NODE)
            continue;
        check_cb(child, elt, elts, i, len);
        ++elt;
    }
}

var display = document.getElementById("display");

function run_series(check_cb) {
    var display = document.getElementById("display");
    // Use a new parent node every time since the optimizations cause
    // bits to be set (permanently) on the parent.
    var p = document.createElement("p");
    display.appendChild(p);
    p.innerHTML = "x<span></span><span></span>";

    check_children(p, check_cb);
    var text = p.removeChild(p.childNodes[0]);
    check_children(p, check_cb);
    var span = p.removeChild(p.childNodes[0]);
    check_children(p, check_cb);
    p.appendChild(span);
    check_children(p, check_cb);
    p.removeChild(span);
    check_children(p, check_cb);
    p.insertBefore(span, p.childNodes[0]);
    check_children(p, check_cb);
    p.removeChild(span);
    check_children(p, check_cb);
    p.insertBefore(span, null);
    check_children(p, check_cb);
    p.appendChild(document.createElement("span"));
    check_children(p, check_cb);
    p.insertBefore(document.createElement("span"), p.childNodes[2]);
    check_children(p, check_cb);
    p.appendChild(text);
    check_children(p, check_cb);

    display.removeChild(p);
}

var style = document.createElement("style");
style.setAttribute("type", "text/css");
var styleText = document.createTextNode("");
style.appendChild(styleText);
document.getElementsByTagName("head")[0].appendChild(style);

styleText.data = "span:first-child { background: lime; }";
run_series(function(child, elt, elts, node, nodes) {
        is(cs(child).backgroundColor, (elt == 0) ? LIME : WHITE,
           "child " + node + " should " + ((elt == 0) ? "" : "NOT ") +
           " match :first-child");
    });

styleText.data = "span:last-child { color: green; }";
run_series(function(child, elt, elts, node, nodes) {
        is(cs(child).color, (elt == elts - 1) ? GREEN : BLACK,
           "child " + node + " should " + ((elt == elts - 1) ? "" : "NOT ") +
           " match :last-child");
    });

styleText.data = "span:only-child { border: medium solid green; }";
run_series(function(child, elt, elts, node, nodes) {
        is(cs(child).borderTopColor, (elts == 1) ? GREEN : BLACK,
           "child " + node + " should " + ((elts == 1) ? "" : "NOT ") +
           " match :only-child");
    });

styleText.data = "span:-moz-first-node { text-decoration: underline; }";
run_series(function(child, elt, elts, node, nodes) {
        is(cs(child).textDecoration, (node == 0) ? "underline" : "none",
           "child " + node + " should " + ((node == 0) ? "" : "NOT ") +
           " match :-moz-first-node");
    });

styleText.data = "span:-moz-last-node { visibility: hidden; }";
run_series(function(child, elt, elts, node, nodes) {
        is(cs(child).visibility, (node == nodes - 1) ? "hidden" : "visible",
           "child " + node + " should " + ((node == nodes - 1) ? "" : "NOT ") +
           " match :-moz-last-node");
    });

styleText.data = "span:nth-child(1) { background: lime; }";
run_series(function(child, elt, elts, node, nodes) {
        var matches = elt == 0;
        is(cs(child).backgroundColor, matches ? LIME : WHITE,
           "child " + node + " should " + (matches ? "" : "NOT ") +
           " match " + styleText.data);
    });

styleText.data = "span:nth-last-child(0n+2) { color: green; }";
run_series(function(child, elt, elts, node, nodes) {
        var matches = (elt == elts - 2);
        is(cs(child).color, matches ? GREEN : BLACK,
           "child " + node + " should " + (matches ? "" : "NOT ") +
           " match " + styleText.data);
    });

styleText.data = "span:nth-of-type(2n+3) { color: green; }";
run_series(function(child, elt, elts, node, nodes) {
        var nidx = elt + 1;
        var matches = nidx % 2 == 1 && nidx >= 3;
        is(cs(child).color, matches ? GREEN : BLACK,
           "child " + node + " should " + (matches ? "" : "NOT ") +
           " match " + styleText.data);
    });

styleText.data = "span:nth-last-of-type(-2n+5) { color: green; }";
run_series(function(child, elt, elts, node, nodes) {
        var nlidx = elts - elt;
        var matches = nlidx % 2 == 1 && nlidx <= 5;
        is(cs(child).color, matches ? GREEN : BLACK,
           "child " + node + " should " + (matches ? "" : "NOT ") +
           " match " + styleText.data);
    });

styleText.data = "span:first-of-type { color: green; }";
run_series(function(child, elt, elts, node, nodes) {
        var matches = (elt == 0);
        is(cs(child).color, matches ? GREEN : BLACK,
           "child " + node + " should " + (matches ? "" : "NOT ") +
           " match " + styleText.data);
    });

styleText.data = "span:last-of-type { color: green; }";
run_series(function(child, elt, elts, node, nodes) {
        var matches = (elt == elts - 1);
        is(cs(child).color, matches ? GREEN : BLACK,
           "child " + node + " should " + (matches ? "" : "NOT ") +
           " match " + styleText.data);
    });

styleText.data = "span:only-of-type { color: green; }";
run_series(function(child, elt, elts, node, nodes) {
        var matches = elts == 1;
        is(cs(child).color, matches ? GREEN : BLACK,
           "child " + node + " should " + (matches ? "" : "NOT ") +
           " match " + styleText.data);
    });

</script>
</pre>
</body>
</html>

